#include "dominion.h"
#include "dominion_helpers.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int testGainCard(int supplyPos, struct gameState *state, int toFlag, int player)
{
	//Note: supplyPos is enum of choosen card
	
	int count;

	//check if supply pile is empty (0) or card is not used in game (-1)
	if ( supplyCount(supplyPos, state) < 1 )
	{
		return -1;
	}

	//added card for [whoseTurn] current player:
	// toFlag = 0 : add to discard
	// toFlag = 1 : add to deck
	// toFlag = 2 : add to hand

	if (toFlag == 1)
	{
		count = state->deckCount[player];
		
		state->deck[ player ][ state->deckCount[player] ] = supplyPos;
		state->deckCount[player]++;
		
		if (state->deckCount[player] == count + 1)
			printf("gainCard(): PASS when test adding to deck\n");
		else
			printf("gainCard(): FAIL when test adding to deck\n");
	}
	else if (toFlag == 2)
	{
		count = state->handCount[player];
		
		state->hand[ player ][ state->handCount[player] ] = supplyPos;
		state->handCount[player]++;
		
		if (state->handCount[player] == count + 1)
			printf("gainCard(): PASS when test adding to hand\n");
		else
			printf("gainCard(): FAIL when test adding to hand\n");
	}
	else
	{
		count = state->discardCount[player];
		
		state->discard[player][ state->discardCount[player] ] = supplyPos;
		state->discardCount[player]++;
		
		if (state->discardCount[player] == count + 1)
			printf("gainCard(): PASS when test adding to discard\n");
		else
			printf("gainCard(): FAIL when test adding to discard\n");
	}
	
	count = state->supplyCount[supplyPos];
		
	//decrease number in supply pile
	state->supplyCount[supplyPos]--;
		
	if (state->supplyCount[supplyPos] == count - 1)
		printf("gainCard(): PASS when test decreasing number in supply pile\n");
	else
		printf("gainCard(): FAIL when test decreasing number in supply pile\n");
	 
	return 0;
}

void test()
{
	struct gameState g;
	struct gameState * game = &g;
	memset(game, 0, sizeof(struct gameState));
	
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, 
		sea_hag, tribute, smithy};
	initializeGame(4, k, 2, game);
	
	testGainCard(10, game, 1, 0);
	testGainCard(10, game, 2, 1);
	testGainCard(10, game, 0, 2);
	
	game->supplyCount[0] = 0;
	if (testGainCard(0, game, 1, 3) == -1)
	{
		printf("gainCard(): PASS when test drawing from empty suppy pile\n");
	}
}

int main(int argc, char *argv[])
{
	test();
	return 0;
}
